package com.tele.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.JsonUtils;
import com.tele.domain.*;
import com.tele.service.ints.*;
import lombok.RequiredArgsConstructor;
//import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@SaCheckLogin
@RequiredArgsConstructor
public class ProductController {
    final CpuService cpuService;
    final DatabaseService databaseService;
    final DiskService diskService;
    final GpuService gpuService;
    final MemService memService;
    final NicService nicService;
    final OsService osService;
    final SupportService supportService;


    @GetMapping("getAllProduct")
    Map getAllProducts() {
        HashMap map = new HashMap();
        map.put("cpus", cpuService.getAllCpu());
        map.put("gpus", gpuService.getAllGpu());
        map.put("mems", memService.getAllMemory());
        map.put("dbs", databaseService.getAllDatabase());
        map.put("disks", diskService.getAllDisk());
        map.put("nics", nicService.getAllNic());
        map.put("oss", osService.getAllOs());
        map.put("supports", supportService.getAllSupport());
        return map;
    }

    @SaCheckRole("admin")
    @GetMapping("deleteProduct")
    Object deleteProduct(String clazz, Long id) {
        if (id == null || clazz == null) {
            return R.fail("Bad Request");
        }
        switch (clazz) {
            case "cpu":
                Cpu cpu = new Cpu();
                cpu.setCpuId(id);
                if (cpuService.delCpu(cpu) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "gpu":
                Gpu gpu = new Gpu();
                gpu.setGpuId(id);
                if (gpuService.delGpu(gpu) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "mem":
                Memory mem = new Memory();
                mem.setMemoryId(id);
                if (memService.delMemory(mem) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "os":
                Os os = new Os();
                os.setOsId(id);
                if (osService.delOs(os) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "db":
                Database db = new Database();
                db.setId(id);
                if (databaseService.delDatabase(db) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "support":
                Support support = new Support();
                support.setId(id);
                if (supportService.delSupport(support) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "nic":
                Nic nic = new Nic();
                nic.setNicId(id);
                if (nicService.delNic(nic) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "disk":
                Disk disk = new Disk();
                disk.setDiskId(id);
                if (diskService.delDisk(disk) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
        }
        return R.fail("产品类型不匹配");
    }

    @SaCheckRole("admin")
    @GetMapping("updateProduct")
    private Object updateProduct(String clazz, Long id, String prod) {
//        System.out.println(prod);
        if (id == null || clazz == null || prod == null) {
            return R.fail("Bad Request");
        }
//        JsonUtils.parseObject(JsonUtils.toJsonString(prod), Cpu.class);
        switch (clazz) {
            case "cpu":
                Cpu cpu = JsonUtils.parseObject(prod, Cpu.class);
                if (cpuService.updateCpu(cpu) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "gpu":
                Gpu gpu = JsonUtils.parseObject(prod, Gpu.class);
                gpu.setGpuId(id);
                if (gpuService.updateGpu(gpu) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "mem":
                Memory mem = JsonUtils.parseObject(prod, Memory.class);
                mem.setMemoryId(id);
                if (memService.updateMemory(mem) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "os":
                Os os = JsonUtils.parseObject(prod, Os.class);
                os.setOsId(id);
                if (osService.updateOs(os) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "db":
                Database db = JsonUtils.parseObject(prod, Database.class);
                db.setId(id);
                if (databaseService.updateDatabase(db) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "support":
                Support support = JsonUtils.parseObject(prod, Support.class);
                if (supportService.updateSupport(support) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "nic":
                Nic nic = JsonUtils.parseObject(prod, Nic.class);
                nic.setNicId(id);
                if (nicService.updateNic(nic) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
            case "disk":
                Disk disk = JsonUtils.parseObject(prod, Disk.class);
                disk.setDiskId(id);
                if (diskService.updateDisk(disk) != 1) {
                    return R.fail("id不匹配");
                }
                return R.ok();
        }
        return R.fail("产品名不匹配");
    }

    @GetMapping("insertProduct")
    @SaCheckRole("admin")
    Object insertProduct(String clazz, String prod) {

        if (clazz == null || prod == null) {
            return R.fail("Bad Request");
        }
//        JsonUtils.parseObject(JsonUtils.toJsonString(prod), Cpu.class);
        switch (clazz) {
            case "cpu":
                Cpu cpu = JsonUtils.parseObject(prod, Cpu.class);
                cpuService.insertCpu(cpu);
                return R.ok(cpu);
            case "gpu":
                Gpu gpu = JsonUtils.parseObject(prod, Gpu.class);
                gpuService.insertGpu(gpu);
                return R.ok(gpu);
            case "mem":
                Memory mem = JsonUtils.parseObject(prod, Memory.class);
                memService.insertMemory(mem);
                return R.ok(mem);
            case "os":
                Os os = JsonUtils.parseObject(prod, Os.class);
                osService.insertOs(os);
                return R.ok(os);
            case "db":
                Database db = JsonUtils.parseObject(prod, Database.class);
                databaseService.insertDatabase(db);
                return R.ok(db);
            case "support":
                Support support = JsonUtils.parseObject(prod, Support.class);
                supportService.insertSupport(support);
                return R.ok(support);
            case "nic":
                Nic nic = JsonUtils.parseObject(prod, Nic.class);
                nicService.insertNic(nic);
                return R.ok(nic);
            case "disk":
                Disk disk = JsonUtils.parseObject(prod, Disk.class);
                diskService.insertDisk(disk);
                return R.ok(disk);
        }
        return R.fail("产品名不匹配");
    }

}
